              package com.java.diagnostics.visualizer.parser.vgc.converters;
              
              import com.java.diagnostics.visualizer.data.axes.UnitConverterDefinition;
              import com.java.diagnostics.visualizer.impl.axes.AbstractUnitConverter;
              import com.java.diagnostics.visualizer.impl.axes.UnitConverterDefinitionImpl;
              import com.java.diagnostics.visualizer.parser.vgc.constants.VGCAxes;
              import java.util.ArrayList;
              import java.util.List;
              
              public class HeapPercentConverter extends AbstractUnitConverter
              {
                private static final int MAX_SEQUENTIAL_TRIES = 4;
                private final List sequenceList;
                private final List heapSizeList;
                private int UNSET = -1;
                private final UnitConverterDefinition definition;
                private static final boolean normalisation = false;
                private double lastAddedHeap = this.UNSET;
              
                private int cachedSequenceUID = 0;
              
                private int cachedIndex = 0;
              
                public HeapPercentConverter(String label)
                {
                  this.sequenceList = new ArrayList();
                  this.heapSizeList = new ArrayList();
                  this.definition = new UnitConverterDefinitionImpl(VGCAxes.BYTES, label, 
                    VGCAxes.PERCENT_SIGN, false);
                }
              
                public void addInflectionPoint(int sequence, double heapSize)
                {
                  if (heapSize != this.lastAddedHeap) {
                    this.sequenceList.add(Integer.valueOf(sequence));
                    this.heapSizeList.add(Double.valueOf(heapSize));
                    this.lastAddedHeap = heapSize;
                  }
                }
              
                public double convert(double number)
                {
                  return this.UNSET;
                }
              
                private int getIndexOfSequenceUID(int sequenceUID)
                {
                  int index = getIndexOfSequenceUIDBySequentialSearch(sequenceUID);
              
                  if (index == this.UNSET)
                  {
                    index = getIndexOfSequenceUIDByBinarySearch(sequenceUID, 0, 
                      this.sequenceList.size());
                  }
              
                  return index;
                }
              
                private int getIndexOfSequenceUIDBySequentialSearch(int sequenceUID)
                {
                  if (this.cachedSequenceUID <= sequenceUID) {
                    int count = 0;
                    int sequence = this.cachedSequenceUID;
                    int index = this.cachedIndex;
                    int previousTime = 0;
                    while ((sequence <= sequenceUID) && 
                      (index < this.sequenceList.size() - 1))
                    {
                      index++;
                      count++;
                      previousTime = sequence;
                      sequence = ((Integer)this.sequenceList.get(index)).intValue();
                    }
              
                    if (sequence > sequenceUID) {
                      this.cachedSequenceUID = previousTime;
                      this.cachedIndex = (index - 1);
                      return this.cachedIndex;
                    }
              
                    if (index >= this.sequenceList.size() - 1) {
                      this.cachedIndex = (index - 1);
                      this.cachedSequenceUID = previousTime;
                      return this.sequenceList.size() - 1;
                    }
              
                  }
              
                  return this.UNSET;
                }
              
                private final int getIndexOfSequenceUIDByBinarySearch(int sequenceUID, int minIndex, int maxIndex)
                {
                  int difference = maxIndex - minIndex;
              
                  if (difference > 4) {
                    int testIndex = minIndex + difference / 2;
                    int newMinIndex = minIndex;
                    int newMaxIndex = maxIndex;
              
                    if (sequenceUID > ((Integer)this.sequenceList.get(testIndex)).intValue())
                      newMinIndex = testIndex;
                    else {
                      newMaxIndex = testIndex;
                    }
              
                    return getIndexOfSequenceUIDByBinarySearch(sequenceUID, newMinIndex, newMaxIndex);
                  }
                  this.cachedIndex = minIndex;
                  if (this.cachedIndex < this.sequenceList.size())
                  {
                    this.cachedSequenceUID = ((Integer)this.sequenceList.get(this.cachedIndex)).intValue();
                    return getIndexOfSequenceUIDBySequentialSearch(sequenceUID);
                  }
              
                  return this.UNSET;
                }
              
                public String formatUnconverted(double number)
                {
                  return String.valueOf((int)number);
                }
              
                public UnitConverterDefinition getDefinition()
                {
                  return this.definition;
                }
              
                public void setOffset(double offset)
                {
                }
              }

